/* $Header: sigbuftask.c,v 2.2 01/10/02 10:29:24 przybyls Exp $ */

/***********************************************************************/
/*                                                                     */
/*                  Copyright Hewlett-Packard Co. 1991                 */
/*                                                                     */
/***********************************************************************/

/***********************************************************************/
/* MODULE NAME: sigbuftask.c                                           */
/* ============                                                        */
/*                                                                     */
/* Author:  Bob Cook                                                   */
/*                                                                     */
/* MODULE PURPOSE: Background NewtOS task which runs the command       */
/*                 processor.                                          */
/*                                                                     */
/*                                                                     */
/***********************************************************************/


/*****************************/
/*   GLOBAL VARIABLES        */
/*****************************/

#include  <stddef.h>
#include  <std_ops.h>
#include  <task_pub.h>

#include <typedefs.h>
#include <p_wkfile.h>
#include <wkfile.h>
#include <ramps.h>
#include <zone_config.h>
#include <zone_status.h>
#include <status.h>

#include <error.h>
#include <diag.h>

#include <cmd_set.h>
#include <mio.h>
#include <cmdproc.h>

#include <i_op.h>

/*****************************/
/*   LOCAL INCLUDE FILES     */
/*****************************/


/*****************************/
/*   GLOBAL VARIABLES        */
/*****************************/


BIT8 Buffer_Status[BUF_STAT_LIM];

/*****************************/
/*   EXTERNAL VARIABLES      */
/*****************************/


extern QUEUE_TYPE Rd_Free_Q;
extern QUEUE_TYPE Wr_Free_Q;
extern QUEUE_TYPE Rd_Input_Q;
extern QUEUE_TYPE Wr_Output_Q[];
extern CMD_QUEUE_TYPE IQ_Cmd_Q;

/*****************************/
/*   LOCAL DEFINES           */
/*****************************/


/*****************************/
/*   STATIC VARIABLES        */
/*****************************/


/*****************************/
/*   IMPLEMENT SECTION       */
/*****************************/


void CheckBufStatus(void)
{
   INTERRUPTS_OFF
   if (Rd_Free_Q.num_entries < Buffer_Status[0]) Buffer_Status[0] = Rd_Free_Q.num_entries;
   if (Wr_Free_Q.num_entries < Buffer_Status[1]) Buffer_Status[1] = Wr_Free_Q.num_entries;
   if (Free_Cmd_Q.num_entries < Buffer_Status[5]) Buffer_Status[5] = Free_Cmd_Q.num_entries;
   if (Rd_Input_Q.num_entries > Buffer_Status[6]) Buffer_Status[6] = Rd_Input_Q.num_entries;
   if (Wr_Output_Q[0].num_entries > Buffer_Status[7]) Buffer_Status[7] = Wr_Output_Q[0].num_entries;
   if (Wr_Output_Q[1].num_entries > Buffer_Status[8]) Buffer_Status[8] = Wr_Output_Q[1].num_entries;
   if (Wr_Output_Q[2].num_entries > Buffer_Status[9]) Buffer_Status[9] = Wr_Output_Q[2].num_entries;
   if (Wr_Output_Q[3].num_entries > Buffer_Status[10]) Buffer_Status[10] = Wr_Output_Q[3].num_entries;
   if (Wr_Output_Q[4].num_entries > Buffer_Status[11]) Buffer_Status[11] = Wr_Output_Q[4].num_entries;
   if (AS_Cmd_Q.num_entries > Buffer_Status[16]) Buffer_Status[16] = AS_Cmd_Q.num_entries;
   if (AX_Cmd_Q.num_entries > Buffer_Status[17]) Buffer_Status[17] = AX_Cmd_Q.num_entries;
   if (CC_Cmd_Q.num_entries > Buffer_Status[18]) Buffer_Status[18] = CC_Cmd_Q.num_entries;
   if (CX_Cmd_Q.num_entries > Buffer_Status[19]) Buffer_Status[19] = CX_Cmd_Q.num_entries;
   if (DT_Cmd_Q.num_entries > Buffer_Status[20]) Buffer_Status[20] = DT_Cmd_Q.num_entries;
   if (DX_Cmd_Q.num_entries > Buffer_Status[21]) Buffer_Status[21] = DX_Cmd_Q.num_entries;
   if (GC_Cmd_Q.num_entries > Buffer_Status[22]) Buffer_Status[22] = GC_Cmd_Q.num_entries;
   if (IQ_Cmd_Q.num_entries > Buffer_Status[23]) Buffer_Status[23] = IQ_Cmd_Q.num_entries;
   if (IX_Cmd_Q.num_entries > Buffer_Status[24]) Buffer_Status[24] = IX_Cmd_Q.num_entries;
   if (OV_Cmd_Q.num_entries > Buffer_Status[25]) Buffer_Status[25] = OV_Cmd_Q.num_entries;
   if (PX_Cmd_Q.num_entries > Buffer_Status[26]) Buffer_Status[26] = PX_Cmd_Q.num_entries;
   if (Q0_Cmd_Q.num_entries > Buffer_Status[27]) Buffer_Status[27] = Q0_Cmd_Q.num_entries;
   if (Q1_Cmd_Q.num_entries > Buffer_Status[28]) Buffer_Status[28] = Q1_Cmd_Q.num_entries;
   if (Q2_Cmd_Q.num_entries > Buffer_Status[29]) Buffer_Status[29] = Q2_Cmd_Q.num_entries;
   if (Q3_Cmd_Q.num_entries > Buffer_Status[30]) Buffer_Status[30] = Q3_Cmd_Q.num_entries;
   if (S1_Cmd_Q.num_entries > Buffer_Status[31]) Buffer_Status[31] = S1_Cmd_Q.num_entries;
   if (S2_Cmd_Q.num_entries > Buffer_Status[32]) Buffer_Status[32] = S2_Cmd_Q.num_entries;
   if (SP_Cmd_Q.num_entries > Buffer_Status[33]) Buffer_Status[33] = SP_Cmd_Q.num_entries;
   if (SS_Cmd_Q.num_entries > Buffer_Status[34]) Buffer_Status[34] = SS_Cmd_Q.num_entries;
   if (TP_Cmd_Q.num_entries > Buffer_Status[35]) Buffer_Status[35] = TP_Cmd_Q.num_entries;
   if (VX_Cmd_Q.num_entries > Buffer_Status[36]) Buffer_Status[36] = VX_Cmd_Q.num_entries;
   if (_0X_Cmd_Q.num_entries > Buffer_Status[37]) Buffer_Status[37] = _0X_Cmd_Q.num_entries;
   if (Inj_Q.num_entries > Buffer_Status[38]) Buffer_Status[38] = Inj_Q.num_entries;
   if (Ping_Q.num_entries > Buffer_Status[39]) Buffer_Status[39] = Ping_Q.num_entries;
   Buffer_Status[40] = Rd_Free_Q.num_entries + Rd_Input_Q.num_entries;
   Buffer_Status[41] = Wr_Free_Q.num_entries + Wr_Output_Q[0].num_entries +
                                               Wr_Output_Q[1].num_entries +
                                               Wr_Output_Q[2].num_entries +
                                               Wr_Output_Q[3].num_entries +
                                               Wr_Output_Q[4].num_entries;
   Buffer_Status[43] = Free_Cmd_Q.num_entries + AS_Cmd_Q.num_entries +
                                                AX_Cmd_Q.num_entries +
                                                CC_Cmd_Q.num_entries +
                                                CX_Cmd_Q.num_entries +
                                                DT_Cmd_Q.num_entries +
                                                DX_Cmd_Q.num_entries +
                                                GC_Cmd_Q.num_entries +
                                                IQ_Cmd_Q.num_entries +
                                                IX_Cmd_Q.num_entries +
                                                OV_Cmd_Q.num_entries +
                                                PX_Cmd_Q.num_entries +
                                                Q0_Cmd_Q.num_entries +
                                                Q1_Cmd_Q.num_entries +
                                                Q2_Cmd_Q.num_entries +
                                                S1_Cmd_Q.num_entries +
                                                S2_Cmd_Q.num_entries +
                                                SP_Cmd_Q.num_entries +
                                                SS_Cmd_Q.num_entries +
                                                TP_Cmd_Q.num_entries +
                                                VX_Cmd_Q.num_entries +
                                               _0X_Cmd_Q.num_entries +
                                                Inj_Q.num_entries +
                                                Ping_Q.num_entries;
   INTERRUPTS_ON
}


void InitBufStatus(void)
{
   /* Initialize max values to zero and min values to 255 */
   /* then call CheckBufStatus() to set current values   */
   Buffer_Status[ 0] = 255;  /* Rd_Free_Q min value */
   Buffer_Status[ 1] = 255;  /* Wr_Free_Q min value */
   Buffer_Status[ 2] =   0;  /* MIO_Rd_Free_Q min value */ /* obsolete */
   Buffer_Status[ 3] =   0;  /* MIO_Wr_Free_Q min value */ /* obsolete */
   Buffer_Status[ 4] =   0;  /* MIO_PGP_Free_Q min value */ /* obsolete */
   Buffer_Status[ 5] = 255;  /* Free_Cmd_Q min value */
   Buffer_Status[ 6] =   0;  /* Rd_Input_Q max value */
   Buffer_Status[ 7] =   0;  /* Wr_Output_Q[0] max value */
   Buffer_Status[ 8] =   0;  /* Wr_Output_Q[1] max value */
   Buffer_Status[ 9] =   0;  /* Wr_Output_Q[2] max value */
   Buffer_Status[10] =   0;  /* Wr_Output_Q[3] max value */
   Buffer_Status[11] =   0;  /* Wr_Output_Q[4] max value */
   Buffer_Status[12] =   0;  /* MIO_Rd_Input_Q max value */ /* obsolete */
   Buffer_Status[13] =   0;  /* MIO_Wr_Output_Q max value */ /* obsolete */
   Buffer_Status[14] =   0;  /* MIO_Wr_Return_Q max value */ /* obsolete */
   Buffer_Status[15] =   0;  /* MIO_PGP_Output_Q max value */ /* obsolete */
   Buffer_Status[16] =   0;  /* AS_Cmd_Q max value */
   Buffer_Status[17] =   0;  /* AX_Cmd_Q max value */
   Buffer_Status[18] =   0;  /* CC_Cmd_Q max value */
   Buffer_Status[19] =   0;  /* CX_Cmd_Q max value */
   Buffer_Status[20] =   0;  /* DT_Cmd_Q max value */
   Buffer_Status[21] =   0;  /* DX_Cmd_Q max value */
   Buffer_Status[22] =   0;  /* GC_Cmd_Q max value */
   Buffer_Status[23] =   0;  /* IQ_Cmd_Q max value */
   Buffer_Status[24] =   0;  /* IX_Cmd_Q max value */
   Buffer_Status[25] =   0;  /* OV_Cmd_Q max value */
   Buffer_Status[26] =   0;  /* PX_Cmd_Q max value */
   Buffer_Status[27] =   0;  /* Q0_Cmd_Q max value */
   Buffer_Status[28] =   0;  /* Q1_Cmd_Q max value */
   Buffer_Status[29] =   0;  /* Q2_Cmd_Q max value */
   Buffer_Status[30] =   0;  /* Q3_Cmd_Q max value */
   Buffer_Status[31] =   0;  /* S1_Cmd_Q max value */
   Buffer_Status[32] =   0;  /* S2_Cmd_Q max value */
   Buffer_Status[33] =   0;  /* SP_Cmd_Q max value */
   Buffer_Status[34] =   0;  /* SS_Cmd_Q max value */
   Buffer_Status[35] =   0;  /* TP_Cmd_Q max value */
   Buffer_Status[36] =   0;  /* VX_Cmd_Q max value */
   Buffer_Status[37] =   0;  /* _0X_Cmd_Q max value */
   Buffer_Status[38] =   0;  /* Inj_Q max value */
   Buffer_Status[39] =   0;  /* Ping_Q max value */

   CheckBufStatus();

}


void SignalBufferTask(void)
{

   while(!POWER_ON_TEST_COMPLETE)
   {
      Task_yield();    /* wait for power on sequence to complete */
   }

   InitBufStatus();


   while (TRUE)                 /* Loop Forever */
   {

      Task_yield ();            /* Yield cpu to rest of background */

      /* run command processor */
      CmdProc();
      /* keep track of buffer usage */
      CheckBufStatus();


   }
}

